<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/2/24
 * Time: 15:43
 */

namespace SDXApi\Service;


use SDXApi\Adapters\HomeTemplateAdapter;
use SDXApi\Model\HomeTemplateModel;
use SDXApi\Model\HomeTemplateStencilModel;

/**
 * Class HomeTemplatesService
 * @package SDXApi\Service
 */
class HomeTemplatesService
{

    /**
     * @var HomeTemplateModel
     */
    protected $templateModel;

    /**
     * @var HomeTemplateStencilModel
     */
    protected $templateStencilModel;

    /**
     * @var
     */
    public $isShow;

    /**
     * @var
     */
    protected $errorMessage;

    /**
     * @var
     */
    protected $userId;

    /**
     * @var
     */
    protected $wid;

    /**
     * HomeTemplatesService constructor.
     * @param int $userId
     * @param int $wid
     */
    public function __construct($userId = 0, $wid = 8)
    {
        $this->templateModel = new HomeTemplateModel();
        $this->templateStencilModel = new HomeTemplateStencilModel();

        $this->userId = empty($userId) ? 0 : $userId + 0;
        $this->wid = empty($wid) ? 0 : $wid + 0;
    }

    /**
     * 获取首页模版
     * Created by Jerry
     * @param $groupId
     * @return array|bool
     */
    public function getHomeTemplateByGroup($groupId)
    {

        $group = $this->findHomeTemplateByGroup($groupId);

        if (empty($group)) {
            return false;
        }

        return $this->checkHomeTemplateGroupData($group);
    }

    /**
     * 获取分组数据
     * Created by Jerry
     * @param $groupId
     * @return bool|mixed
     */
    protected function findHomeTemplateByGroup($groupId)
    {
        if (empty($groupId)) {
            return false;
        }

        return $this->templateModel->find_home_template_by_group_id($groupId, $this->isShow);
    }

    /**
     * 处理首页分组数据
     * Created by Jerry
     * @param $data
     * @return array|bool
     */
    protected function checkHomeTemplateGroupData($data)
    {
        if (empty($data)) {
            return false;
        }

        $templateData = $this->findHomeTemplateByParentId($data['id']);

        if (empty($templateData) && $this->isShow) {
            $stencil = $this->findTemplateStencilByTemplateId($data['id']);
        }
        else{
            $stencil = $this->findTemplateStencilByTemplateId($templateData['id']);
        }

        return $this->templateStencil($stencil);
    }

    /**
     * Created by Jerry
     * @param $parentId
     * @return mixed
     */
    protected function findHomeTemplateByParentId($parentId)
    {
        return $this->templateModel->find_home_template_by_parent_id($parentId, $this->isShow);
    }

    /**
     * Created by Jerry
     * @param $templateId
     * @return mixed
     */
    protected function findTemplateStencilByTemplateId($templateId)
    {
        return $this->templateStencilModel->find_template_stencil_by_template_id($templateId);
    }

    /**
     * 循环处理模块
     * Created by Jerry
     * @param $stencils
     * @return array
     */
    protected function templateStencil($stencils)
    {
        if (empty($stencils)) {
            $this->setErrorMessage('暂无模块数据');
        }

        $data = array();
//		判断是否新用户
        $isNewUser = $this->getIsNewUser($this->userId);

        foreach ($stencils as $key => $value) {
//          老用户不显示
            if (($value['is_new_user'] == 1) && $isNewUser != 1) {
                continue;
            }
//          新用户不显示
            if (($value['is_new_user'] == 2) && $isNewUser != 0) {
                continue;
            }

            //如果已经领取了满99元减少50的红包，则不显示新用户专享
            if ($value['name'] == '新用户专享' && !$this->show_bonus_new($this->userId)) {
                continue;
            }

            $stencil = $this->stencil($value['type'])->handle($value, $this->userId, $this->wid);

            if (empty($stencil)) {
                continue;
            }

            $stencil['border_size'] = 20;


            $data[] = $stencil;
        }

        return $data;
    }

    /**
     * 检查是否领取了满99元减50红包
     * @param $user_id
     * @return bool
     */
    private function show_bonus_new($user_id){
        $map = array('user_id'=>$user_id,'bonus_type_id'=>162);
        $count = M('user_bonus')->where($map)->count();
        if($count>0){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 判断是否为新用户
     * Created by Jerry
     * @param int $user_id
     * @return mixed
     */
    protected function getIsNewUser($user_id = 0)
    {
        if (empty($user_id)) {
            return false;
        }

        $isNewUser = D('Orderinfo')->isNewUser($user_id);

        if ($isNewUser == false) {
            return 0;
        }

        return 1;
    }

    /**
     * 处理模块数据
     * Created by Jerry
     * @param int $type
     * @return HomeTemplateAdapter
     */
    protected function stencil($type = 0)
    {
        return new HomeTemplateAdapter($type);
    }

    /**
     * 设置错误信息
     * Created by Jerry
     * @param $message
     * @return $this
     */
    protected function setErrorMessage($message)
    {
        array_push($this->errorMessage, $message);
        return $this;
    }

    /**
     * 返回专题名字
     * Created by Jerry
     * @param $groupId
     * @return bool|mixed
     */
    public function getHomeTemplateGroupName($groupId)
    {
        $group = $this->findHomeTemplateByGroup($groupId);

        if (empty($group)) {
            return false;
        }

        $templateParent = $this->findHomeTemplateByParentId($group['id']);

        if (empty($templateParent) && $this->isShow) {
            return $group;
        }

        return $templateParent;
    }
}